import { apiClient } from './client'
import type {
  WishlistPageResponse,
  WishlistCheckResponse,
  WishlistCountResponse,
  WishlistAddRequest,
  WishlistDeleteRequest,
  Collection
} from '@/types/wishlist'

// 添加收藏
export const addToWishlist = (shoeId: number, userId: number) => {
  // 尝试使用FormData格式，因为后端可能期望表单数据
  const formData = new FormData()
  formData.append('shoeId', String(shoeId))
  formData.append('userId', String(userId))
  
  return apiClient.post<{ code: number; msg: string; data: boolean }>('/api/v1/collections/add', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除收藏
export const removeFromWishlist = (shoeId: number, userId: number) => {

  const params = new URLSearchParams()
  params.append('shoeId', String(shoeId))
  params.append('userId', String(userId))
  
  return apiClient.delete<{ code: number; msg: string; data: boolean }>(`/api/v1/collections/delete`, {params})
}

// 根据收藏ID删除收藏
export const removeFromWishlistById = (collectionId: number) => {
  return apiClient.delete<{ code: number; msg: string; data: boolean }>(`/api/v1/collections/delete/${collectionId}`)
}

// 检查收藏状态
export const checkWishlistStatus = (userId: number, shoeId: number) => {
  return apiClient.get<WishlistCheckResponse>('/api/v1/collections/check', {
    params: { userId, shoeId }
  })
}

// 获取用户收藏数量
export const getWishlistCount = (userId: number) => {
  return apiClient.get<WishlistCountResponse>(`/api/v1/collections/count/${userId}`)
}

// 获取收藏详情
export const getWishlistDetail = (userId: number, shoeId: number) => {
  return apiClient.get<{ code: number; msg: string; data: Collection }>('/api/v1/collections/detail', {
    params: { userId, shoeId }
  })
}

// 获取用户收藏列表（分页）
export const getWishlistPage = (userId: number, pageNum = 1, pageSize = 10) => {
  return apiClient.get<WishlistPageResponse>(`/api/v1/collections/user/${userId}/page`, {
    params: { pageNum, pageSize }
  })
}

// 获取用户收藏列表（不分页）
export const getWishlistList = (userId: number) => {
  return apiClient.get<{ code: number; msg: string; data: any[] }>(`/api/v1/collections/user/${userId}`)
}
